package com.xiaoxu.privatechat.controller.admin;

import com.xiaoxu.privatechat.bean.CodeMsg;
import com.xiaoxu.privatechat.bean.Result;
import com.xiaoxu.privatechat.entity.admin.Menu;
import com.xiaoxu.privatechat.service.admin.MenuService;
import com.xiaoxu.privatechat.service.admin.OperaterLogService;
import com.xiaoxu.privatechat.util.MenuUtil;
import com.xiaoxu.privatechat.util.ValidateEntityUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author xiaoxu
 * @description 后台菜单管理控制器
 * @date 2023/2/13
 */
@RequestMapping("/admin/menu")
@Controller
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private OperaterLogService operaterLogService;

    /**
     * @description 菜单列表展示页面
     * @param model 模型
     * @return String
     */
    @RequestMapping(value = "/list")
    public String list(Model model) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("title", "菜單列表");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        model.addAttribute("thirdMenus", MenuUtil.getThirdMenus(findAll));
        return "admin/menu/list";
    }

    /**
     * @description 菜单添加页面
     * @param model 模型
     * @return String
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("title", "菜單列表");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        return "admin/menu/add";
    }

    /**
     * @description 菜单添加提交表单处理
     * @param menu 权限菜单
     * @return Result
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Menu menu) {
        if (menu == null) {
            Result.error(CodeMsg.DATA_ERROR);
        }
        //用统一验证实体方法验证是否合法
        Result<Boolean> validate = getBooleanResult(menu);
        if (validate != null) return validate;
        //表示验证都通过，开始添加数据库
        if (menuService.save(menu) == null) {
            Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("添加選單資訊【" + menu + "】");
        return Result.success(true);
    }

    /**
     * @description 菜单编辑页面
     * @param model 模型
     * @param id 权限id
     * @return String
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String eidt(Model model, @RequestParam(name = "id") Long id) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("title", "菜單列表");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        model.addAttribute("menu", menuService.find(id));
        return "admin/menu/edit";
    }

    /**
     * @description 菜单编辑页面表单提交处理
     * @param menu 菜单权限
     * @return Result
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(Menu menu) {
        if (menu == null) {
            Result.error(CodeMsg.DATA_ERROR);
        }
        if (Objects.requireNonNull(menu).getId() == null) {
            Result.error(CodeMsg.ADMIN_MENU_ID_EMPTY);
        }
        //用统一验证实体方法验证是否合法
        Result<Boolean> validate1 = getBooleanResult(menu);
        if (validate1 != null) return validate1;
        Menu existMenu = menuService.find(menu.getId());
        if (existMenu == null) {
            Result.error(CodeMsg.ADMIN_MENU_ID_ERROR);
        }
        //表示验证都通过，开始添加数据库
        Objects.requireNonNull(existMenu).setIcon(menu.getIcon());
        existMenu.setName(menu.getName());
        existMenu.setParent(menu.getParent());
        existMenu.setSort(menu.getSort());
        existMenu.setUrl(menu.getUrl());
        existMenu.setButton(menu.isButton());
        existMenu.setShow(menu.isShow());
        if (menuService.save(existMenu) == null) {
            Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("編輯選單資訊【" + existMenu + "】");
        return Result.success(true);
    }

    /**
     * @description 验证传输数据
     * @param menu 菜单权限
     * @return Result
     */
    private Result<Boolean> getBooleanResult(Menu menu) {
        CodeMsg validate = ValidateEntityUtil.validate(menu);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        if (menu.getParent() != null) {
            if (menu.getParent().getId() == null) {
                menu.setParent(null);
            }
        }
        return null;
    }

    /**
     * @description 删除菜单信息
     * @param id 菜单id
     * @return Result
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id") Long id) {
        try {
            menuService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_MENU_DELETE_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("删除選單資訊，選單ID【" + id + "】");
        return Result.success(true);
    }
}
